function bcc = bcc2area(bvqxfolder)
% bcc2area  - lookup TalairachBrain.vmr color codes
%
% FORMAT:       bcc = bcc2area(BVQXfolder)
%
% Input fields:
%
%       BVQXfolder  path name where TalairachBrain.vmr is stored
%
% Output fields:
%
%       bcc         255x1 cell array with text descriptions
%
% See also BVQXfile

% Version:  v0.8a
% Build:    9102221
% Date:     Oct-22 2009, 3:43 PM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% cd to correct folder and load TalairachBrain.vmr
try
    cd(bvqxfolder);
    tvo = BVQXfile('TalairachBrain.vmr');
    tv = tvo.VMRData(:, :, :);
    tvbb = tvo.BoundingBox;
    tvo.ClearObject;
catch
    error( ...
        'BVQXtools:BadArgument', ...
        'No or bad BVQX installation folder given.' ...
    );
end

% loop over colorcodes
mmm = minmaxmean(tv(tv > 0));
bcc = cell(255, 1);
for cc = mmm(1):mmm(2)

    % say for which color code...
    ccout = {sprintf('Results for color code %d:', cc)};
    disp(ccout{1});

    % find voxels
    tal = bvcoordconv(find(tv(:) == uint8(cc)), 'bvx2tal', tvbb);
    cend = size(tal, 1);

    % prepare arrays
    ttd  = cell(cend, 1);
    ttdr = cell(cend, 5);

    % lookup each coordinate
    for c = 1:cend

        % use tdclient with NGM and 5 mm radius search
        ttd{c} = tdlocal(5, tal(1,c), tal(2,c), tal(3,c), [5, 1]);

        % keep user updated
        if ~mod(c, 1000)
            disp(sprintf('%d from %d coords looked up...', c, cend));
            drawnow;
        end
    end

    % get unique names
    for c = 1:cend

        % remove non-used part
        ttdr(c, :) = splittocell(regexprep(regexprep( ...
            ttd{c}, '^.*\:\s+', ''), '\s+(\(coo.*)?$', ''), ',');

        % glue rest together again
        ttd{c} = gluetostring(ttdr(c, :), ',');
    end
    u = unique(ttd);

    % print number of voxels and 
    ccout{1 + numel(u)} = '';
    for c = 1:numel(u)
        ccout{c + 1} = sprintf('%5d vx: %s', ...
            length(find(strcmp(ttd, u{c}))), u{c});
    end

    % put ccout in correct cell
    bcc{cc} = ccout;
end

% delete empty cells
bcc(isemptycell(bcc)) = [];
